package dryven.util.json.dryvenjson.encoders;

import java.io.IOException;

import dryven.model.di.LocalThreadStorage;
import dryven.util.json.JsonEncoder;
import dryven.util.json.dryvenjson.FieldPolicy;
import dryven.util.json.dryvenjson.JSONCompositeEncoder;
import dryven.util.json.dryvenjson.JSONTypeEncoder;

public class StringEncoder implements JSONTypeEncoder {

	private GsonStringEscaper _escaper = new GsonStringEscaper(false);
	
	@Override
	public void encodeValue(Object value, JSONCompositeEncoder e, FieldPolicy p, LocalThreadStorage diStorage, Appendable dest) throws IOException {
		String text = _escaper.escapeJsonString(value.toString());
		dest.append('"');
		dest.append(text);
		dest.append('"');
	}

	@Override
	public boolean isApplicableForType(Class<?> type) {
		return CharSequence.class.isAssignableFrom(type);
	}

}
